package com.atsmart.app.model;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.view.Gravity;
import android.widget.Toast;

import com.atsmart.app.R;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.ItemizedOverlay;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.OverlayItem;

public class MapMarker extends ItemizedOverlay<OverlayItem> {
    private Context context;
    private List<OverlayItem> points = new ArrayList<OverlayItem>();
    private ClientListInfo clients;

    public MapMarker(Context context, ClientListInfo clients) {
        super(boundCenterBottom(context.getResources().getDrawable(R.drawable.iconmarka)));
        this.context = context;
        this.clients = clients;
        GeoPoint point;
        if (clients != null && clients.getClients().size() > 0) {
            for(ClientInfo client: clients.getClients()){
                point = new GeoPoint((int) (client.getLat() * 1E6), (int) (client.getLon() * 1E6));
                points.add(new OverlayItem(point, client.getName(), client.getAddress()));
            }
        }


        populate();
    }

    @Override
    protected OverlayItem createItem(int position) {
        return points.get(position);
    }

    @Override
    public int size() {
        return points.size();
    }

    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {
    	super.draw(canvas, mapView, shadow);
//    	Projection projection = mapView.getProjection();
//        for (int index = this.size() - 1; index >= 0; index--) {
//            OverlayItem overLayItem = getItem(index);
//            Point point = projection.toPixels(overLayItem.getPoint(), null);
//            Paint paintText = new Paint();
//            paintText.setColor(Color.RED);
//            paintText.setTextSize(13);
//            canvas.drawText(overLayItem.getTitle(), point.x + 10, point.y - 15, paintText);
//        }
    }

    @Override
    protected boolean onTap(int position) {
    	Toast toast = Toast.makeText(context, clients.getClients().get(position).getName(), Toast.LENGTH_SHORT);
    	toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 0);
        toast.show();
        return true;
    }
}